package org.itboys.mobile.service.mongo.web.dynamic;

import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.dynamic.SundayDynamic;
import org.itboys.mobile.entity.mongo.dynamic.SundayDynamicOperate;
import org.itboys.mobile.entity.mongo.dynamic.SundayDynamicProduct;
import org.itboys.mobile.util.NameUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年4月26日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_移动端（通用）_动态_点赞或评论_sevice。如果属性不够，请自行填充
 */
@Service
public class SundayDynamicOperateService extends BaseMongoService<SundayDynamicOperate>{
    @Autowired
    private SundayDynamicService dynamicService;
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayDynamicOperate> getEntityClass() {
        return  SundayDynamicOperate.class;
    }

    /**
     * 分页查询动态_评论或点赞
     * @param request
     * @param type
     * @return
     */
    public PageResult<SundayDynamicOperate> selectOperate(HttpServletRequest request, Integer type,Long dynamicId) {
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("type",type);
        param.put("dynamicId",dynamicId);
        Map<String, String> containsparam =ParamUtil.packageMongoVagueParam(request,"memberName","content");
        return super.containpageQuery(request,containsparam,param);
    }

    /**
     * 点赞或评论
     * @param dynamicId
     * @param type
     * @param content
     */
    public void operate(Long dynamicId,Integer type,String content){
        //修改动态主体的点赞和评论数量
        SundayDynamic dynamic = dynamicService.getById(dynamicId);
        if(type==1){
            dynamic.setDzNumber(dynamic.getDzNumber()+1);
        }else if(type==2){
            dynamic.setPlNumber(dynamic.getPlNumber()+1);
        }
        dynamicService.updateExceptEmpty(dynamicId,dynamic);
        //增减动态的评论或点赞记录
        SundayDynamicOperate dynamicOperate = new SundayDynamicOperate();
        dynamicOperate.setDynamicId(dynamicId);
        dynamicOperate.setType(type);
        dynamicOperate.setMemberId(0l);
        dynamicOperate.setMemberName(NameUtil.getRandomName());
        dynamicOperate.setMemberImage(NameUtil.getRandomLogo());
        dynamicOperate.setContent(content);
        super.saveWithoutLogin(dynamicOperate);
    }

    /**
     * 删除评论或点赞
     * @param id
     */
    public void deleteOperate(long id){
        SundayDynamicOperate operate= super.getById(id);
        super.delete(id);
        SundayDynamic dynamic=dynamicService.getById(operate.getDynamicId());
        if(dynamic.getType()==1){
            dynamic.setDzNumber(dynamic.getDzNumber()-1);
        }else if(dynamic.getType()==2){
            dynamic.setPlNumber(dynamic.getPlNumber()-1);
        }
    }
}
